package srzy.driver

import scala.collection.mutable.ListBuffer
import srzy.diameter._
import srzy.common.Logging
import scala.xml.Node
import java.io.File

object Start9 extends Logging {

  def main(args: Array[String]): Unit = {
    deal

    //    deal2("""case/data/2.1.0_1.15_data.xml""")
  }

  def deal = {
    val l = new File("case")
      .listFiles()
      .filter(_.isDirectory)
      .flatMap(x => x.listFiles())
      .filter(_.getName().takeRight(3) == "xml")
      .map(_.getPath())
    l.foreach(x => {
      logger.debug("deal : " + x)
      deal2(x)
    })
  }

  def deal2(filePath: String = "case/in/sz_in_1201.xml") = {
    val file = srzy.common.readFromFile(filePath).drop(1)
    val traffic = (scala.xml.XML.loadString(file.mkString("\n")) \\ "traffic").head

    //    println("traffic.child.toList:" + traffic.toString)

    var initFlag = true
    val blocks = traffic.nonEmptyChildren.toList.foldLeft(scala.xml.NodeSeq.Empty)((l, x) => {
      x.label match {
        case "send" =>
          initFlag = false
          val send = (x \ "command").head
          l ++ <send channel="channel-1"> { send.toBytes.toDCCNode }</send>
        case "receive" =>
          l ++ <receive channel="channel-1">
                 <command name="CCA"></command>
               </receive>

        //        case "RBSleep" =>
        //          l.append(new srzy.module.RBMsgSleepAction {
        //            val time = Integer.parseInt((x \\ "@value").head.text)
        //          })
        //          l
        //
        //        case "RBSleepAndDiscard" =>
        //          l ++ <RBSleepAndDiscard value={ 1 }></RBSleepAndDiscard>
        //
        //        case "db" =>
        //          val str = (x \\ "@name").head.text
        //          l.append(new srzy.module.DbOperAction {
        //            val source = str
        //            val dbUtil = DBUtil(str)
        //            val sqlList = (x \ "sql").toList.map(x => x.text)
        //          })
        //          l
        //
        //        case "cache" =>
        //          //            val str = (x \\ "@name").head.text
        //          l.append(new srzy.module.CacheOperAction {
        //            val sqlList = (x \ "cachesql").toList.map(x => x.text)
        //          })
        //          l
        //        case "CleanCache" =>
        //          l.append(new srzy.module.CleanCacheAction {})
        //          l
        //
        //        case "shell" =>
        //          l.append(new srzy.module.ShellOperAction {
        //            val shell = x.text
        //          })
        //          l
        //        case "refreshSM" =>
        //          l.append(new srzy.module.ShellOperAction {
        //            val shell = x.text
        //          })
        //          l
        //

        case _ =>
          val r1 = """[\p{Space}]+""".r

          if (r1.pattern.matcher(x.toString).matches() == false) {
            if (initFlag == true)
              logger.debug("\n" + x)
          }
          l ++ x

      }
    })

    //    logger.debug("blocks:" + blocks)

    val scenario = <scenario>
                     <init>
                       <send channel="channel-1">
                         <command name="CER">
                           <avp name="Origin-Host" value="sm-all-test"></avp>
                           <avp name="Origin-Realm" value="chinaunicom.com"></avp>
                           <avp name="Host-IP-Address" value="0x000100000000"></avp>
                           <avp name="Vendor-Id" value="11"></avp>
                           <avp name="Product-Name" value="HP Cx Interface"></avp>
                           <avp name="Origin-State-Id" value="62"></avp>
                           <avp name="Supported-Vendor-Id" value="10415"></avp>
                           <avp name="Auth-Application-Id" value="4"></avp>
                           <avp name="Acct-Application-Id" value="0"></avp>
                           <avp name="Firmware-Revision" value="1"></avp>
                         </command>
                       </send>
                       <receive channel="channel-1">
                         <command name="CEA"></command>
                       </receive>
                     </init>
                     <traffic>
                       { blocks }
                     </traffic>
                   </scenario>

    val llll = new srzy.common.PrettyPrinter(800, 2)

    val txt = """<?xml version="1.0" encoding="iso-8859-1"?>""" + "\n" + llll.format(scenario)

    //    logger.debug("txt:" + txt)
    srzy.common.writeToFile("new" + filePath, txt)
  }
}